package sige

import org.springframework.dao.DataIntegrityViolationException
import sige.BimestreService

class SemestreController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        [semestreInstanceList: Semestre.list(params), semestreInstanceTotal: Semestre.count()]
    }
    
    def create_1(Long id) {
        if(!id){
            redirect( controller: "anoLetivo" , action: "create")
            return
        }
        def ano = AnoLetivo.get (id)
        def semestre
        if(!ano.semestre_1){
            semestre = new Semestre(params)
            semestre.ano = ano
        }else{
            semestre = ano.semestre_1
        }
        [semestreInstance: semestre]
    }
    
    def create_2(Long id) {
        if(!id){
            redirect( controller: "anoLetivo" , action: "create")
            return
        }
        def ano = AnoLetivo.get (id)
        def semestre
        if(!ano.semestre_2){
            semestre = new Semestre(params)
            semestre.ano = ano
        }else{
            semestre = ano.semestre_2
        }
        [semestreInstance: semestre]
    }
    
    def save1(Long id){
        
        def semestreInstance
        if(id){
            semestreInstance = Semestre.get(id)
            semestreInstance.properties = params
        }else{
            semestreInstance = new Semestre(params)
        }
        
        semestreInstance.nome = "1° Semestre"
        semestreInstance.ano.semestre_1 = semestreInstance
        
        def semestreService = new SemestreService()
        semestreService.verificarDatas(semestreInstance)

        if(semestreInstance.hasErrors())
        {
            println(semestreInstance.errors)
            render(view: "create_1", model: [semestreInstance: semestreInstance])
            return
        }
        else if (!semestreInstance.save()) {
            render(view: "create_1", model: [semestreInstance: semestreInstance])
            return
        }
        
        def anoLetivoInstance = semestreInstance.ano
        
        anoLetivoInstance.save(flush: true)
        
        flash.message = message(code: 'default.created.message', args: [message(code: 'semestre.label', default: 'Semestre'), semestreInstance.id])
        redirect(action: "createBimestres", id: semestreInstance.id ) 
    }
    
    
    def save2(Long id){
        def semestreInstance
        if(id){
            semestreInstance = Semestre.get(id)
            semestreInstance.properties = params
        }else{
            semestreInstance = new Semestre(params)
        }
        
        semestreInstance.nome = "2° Semestre"
        semestreInstance.ano.semestre_2 = semestreInstance
        
            
        def semestreService = new SemestreService()
        semestreService.verificarDatas(semestreInstance)

        if(semestreInstance.hasErrors())
        {
            println(semestreInstance.errors)
            render(view: "create_2", model: [semestreInstance: semestreInstance])
            return
        }
        else if (!semestreInstance.save()) {
            render(view: "create_2", model: [semestreInstance: semestreInstance])
            return
        }
        
        def anoLetivoInstance = semestreInstance.ano
        
        anoLetivoInstance.save(flush: true)
        
        flash.message = message(code: 'default.created.message', args: [message(code: 'semestre.label', default: 'Semestre'), semestreInstance.id])
        redirect(action: "createBimestres1", id: semestreInstance.id ) 
    }
    
    def show(Long id) {
        def semestreInstance = Semestre.get(id)
        if (!semestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "list")
            return
        }

        [semestreInstance: semestreInstance]
    }

    def edit(Long id) {
        def semestreInstance = Semestre.get(id)
        if (!semestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "list")
            return
        }

        [semestreInstance: semestreInstance]
    }

    def update(Long id, Long version) {
        def semestreInstance = Semestre.get(id)
        if (!semestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (semestreInstance.version > version) {
                semestreInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'semestre.label', default: 'Semestre')] as Object[],
                          "Another user has updated this Semestre while you were editing")
                render(view: "edit", model: [semestreInstance: semestreInstance])
                return
            }
        }

        semestreInstance.properties = params

        if (!semestreInstance.save(flush: true)) {
            render(view: "edit", model: [semestreInstance: semestreInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'semestre.label', default: 'Semestre'), semestreInstance.id])
        redirect(action: "show", id: semestreInstance.id)
    }

    def delete(Long id) {
        def semestreInstance = Semestre.get(id)
        if (!semestreInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "list")
            return
        }

        try {
            semestreInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'semestre.label', default: 'Semestre'), id])
            redirect(action: "show", id: id)
        }
    }
    
    def createBimestres(Long id){
        if(!id){
            redirect( controller: "anoLetivo" , action: "create")
            return
        }
        def semestre = Semestre.get(id)
        
        [semestreInstance: semestre]   
    }
    
    def createBimestres1(Long id){
        if(!id){
            redirect( controller: "anoLetivo" , action: "create")
            return
        }
        def semestre = Semestre.get(id)
        
        [semestreInstance: semestre]  
    }
    
    def saveBimestres(Long id){
        def semestreInstance = Semestre.get(id)
        Bimestre bimestre1Instance = new Bimestre()
        Bimestre bimestre2Instance = new Bimestre()
        
        semestreInstance.bimestre_1 = bimestre1Instance
        semestreInstance.bimestre_2 = bimestre2Instance
        
        bimestre1Instance.errors = null
        bimestre2Instance.errors = null
        
        semestreInstance.properties = params
        
        def bimestreService = new BimestreService()
        bimestreService.verificarDatas(semestreInstance)

        semestreInstance.save()
       
        if(bimestre1Instance.hasErrors() || bimestre2Instance.hasErrors())
        {
            render(view: "createBimestres", model: [semestreInstance: semestreInstance])
            flash.message = "Erro entre as Datas"
            bimestre1Instance.save()
            bimestre2Instance.save()
            return
        }else if (!bimestre1Instance.save() || !bimestre2Instance.save(flush:true)) {
            render(view: "createBimestres", model: [semestreInstance: semestreInstance])
            return
        }
        
        redirect(action: "create_2", 'id': semestreInstance.ano.id) 
    }
    
    def saveBimestres1(Long id){
        def semestreInstance = Semestre.get(id)
        
        semestreInstance.bimestre_1 = new Bimestre()
        semestreInstance.bimestre_2 = new Bimestre()
        
        semestreInstance.bimestre_1.errors = null
        semestreInstance.bimestre_2.errors = null
        
        semestreInstance.properties = params
        
        def bimestreService = new BimestreService()
        bimestreService.verificarDatas(semestreInstance)
        
        if( semestreInstance.bimestre_1.hasErrors() || semestreInstance.bimestre_2.hasErrors())
        {
            render(view: "createBimestres1", model: [semestreInstance: semestreInstance])
            flash.message = "Erro entre as Datas"
            semestreInstance.bimestre_1.save()
            semestreInstance.bimestre_2.save()
            return
        }
        
        semestreInstance.bimestre_1.save()
        semestreInstance.bimestre_2.save()
        semestreInstance.save(flush:true)
        
        redirect(controller: "anoLetivo", action: "show", 'id': semestreInstance.ano.id) 
    }
}
